
/* create a calendar popup */
var popUpCalendar = function (objid, pages, locale, returnFunction) {
    this.callerObject = null;
    // glue container
	var el = new YAHOO.util.Dom.get(objid);
	var nel = document.createElement("div");
	nel.style.position = 'absolute';
	nel.style.zIndex = '1002';
	nel.id = objid + '_cal';
	// insert in document.body as first element
	YAHOO.util.Dom.insertBefore(nel, el);

    this.objectid = objid;
    this.cointainerid = nel.id;

    this.over_cal = false;
    this.calobj = new YAHOO.widget.CalendarGroup("cal" + objid,this.cointainerid,
    {
        START_WEEKDAY: 1,
        PAGES:(pages ? pages : 1),
        navigator:true
    });
    if (locale == 'de'){
	    this.calobj.cfg.setProperty("MONTHS_SHORT",   ["Jan", "Feb", "M\u00E4r", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]);
	    this.calobj.cfg.setProperty("MONTHS_LONG",    ["Januar", "Februar", "M\u00E4rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"]);
	    this.calobj.cfg.setProperty("WEEKDAYS_1CHAR", ["S", "M", "D", "M", "D", "F", "S"]);
	    this.calobj.cfg.setProperty("WEEKDAYS_SHORT", ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"]);
	    this.calobj.cfg.setProperty("WEEKDAYS_MEDIUM",["Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam"]);
	    this.calobj.cfg.setProperty("WEEKDAYS_LONG",  ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]);
    }
    if (!returnFunction){
        returnFunction = function () {};
    }
    this.returnFunction = returnFunction;

    this.showCal = function (e, obj) {
        if (this.cointainerid)
            obj = this;
        var xy = YAHOO.util.Dom.getXY(this.id);
        var datum = YAHOO.util.Dom.get(this.id).value.split('.');
        if (!datum || datum == "" || datum.length != 3) {
            var Zeit = new Date();
            datum[1] = Zeit.getMonth()+1;
            datum[0] = Zeit.getDate();
            datum[2] = Zeit.getFullYear();
        }
        var date = datum[1] + '/' + datum[0] + '/' + datum[2];
        if (date != '//') {
            obj.calobj.cfg.setProperty('selected', date, false);
            obj.calobj.cfg.setProperty('pagedate', datum[1] + '/' + datum[2], false);
            obj.calobj.render();
        }
        YAHOO.util.Dom.setStyle(obj.cointainerid, 'display', 'block');
        xy[1] = xy[1] + 20;
        YAHOO.util.Dom.setXY(obj.cointainerid, xy);
    };
    this.hideCal = function (e, obj) {
        if (this.cointainerid)
            obj = this;
		if (!obj.over_cal)
        	YAHOO.util.Dom.setStyle(obj.cointainerid, 'display', 'none');
    };
    this.getDate = function (e, obj) {
        if (this.cointainerid)
            obj = this;
        var calDate = obj.calobj.getSelectedDates()[0];
        calDate = calDate.getDate() + '.' +  (calDate.getMonth() + 1) + '.' + calDate.getFullYear();
        YAHOO.util.Dom.get(obj.objectid).value = calDate;
        obj.over_cal = false;
        obj.hideCal();
        obj.returnFunction(obj);
	};
	this.setupListeners = function(e, obj) {
		if (this.cointainerid)
            obj = this;
    	YAHOO.util.Event.addListener(obj.cointainerid, 'mouseover', obj.overCal, obj);
    	YAHOO.util.Event.addListener(obj.cointainerid, 'mouseout', obj.outCal, obj);
	};
	this.overCal = function (e, obj) {
		if (this.cointainerid)
            obj = this;
    	obj.over_cal = true;
	};

	this.outCal = function (e, obj) {
    	if (this.cointainerid)
            obj = this;
    	obj.over_cal = false;
	};
    this.calobj.selectEvent.subscribe(this.getDate, this.calobj, this);
    this.calobj.renderEvent.subscribe(this.setupListeners, this.calobj, this);
    YAHOO.util.Event.addListener(this.objectid, 'focus', this.showCal, this);
    YAHOO.util.Event.addListener(this.objectid, 'blur', this.hideCal, this);
    this.calobj.render(this.objectid);
    this.hideCal();
    return this;
}
/**
 * make a object editable (textbox) with calendar
 *
 * @param object   srcObject        object to edit
 * @param function callbackFunction function to call on edit finish (objectId, newValue)
 * @returns
 */
var yuiInlineEditCalendar = function (srcObject, callbackFunction){
    this.element = new yuiInlineEdit(srcObject, callbackFunction);
    this.applyCalendar = function (obj) {
        obj.callerObject.element.applyClick(null, obj.callerObject.element);
    };
    this.cal = new popUpCalendar(this.element.fieldId, 1, "de", this.applyCalendar);
    this.cal.callerObject = this;
    YAHOO.util.Dom.get(this.element.fieldId).blur();
    YAHOO.util.Dom.get(this.element.fieldId).focus();
    return this.element;
}